<?php
require '../$header.php';
	
	$error = false; 
	$return = array();
	
	$Email = $DB->escape($_POST['Email'], 50);
	$Password = $DB->escape($_POST['Password'], 32);
	$FirstName = $DB->escape($_POST['FirstName'], 32);
	$LastName = $DB->escape($_POST['LastName'], 32);
	
	if(strlen($Email) == 0)
	{
		$error = true; 
		$return['Error']  = "Please Enter an Email";
	}
	
	if(!$error && strlen($Password) == 0)
	{
		$error = true; 
	$return['Error']  = "Please Enter a Password";
	}
	
	if(!$error && strlen($FirstName) == 0)
	{
		$error = true; 
		$return['Error']  = "Please Enter a First Name";
	}
	
	if(!$error && strlen($LastName) == 0)
	{
		$error = true; 
		$return['Error']  = "Please Enter a Last Name";
	}
	
	//check fi this email exists
	if(!$error)
	{
		$DB->query("SELECT NULL FROM user WHERE Email = '{$Email}'");
		
		if($DB->num() != 0)
		{
			$error = true; 
			$return['Error']  = "This email address is already in use";
		}
	}
	
	if(!$error)
	{
		$Password = md5($Password);
		
		$DB->query("
			INSERT INTO user
			(
				Email,
				Password,
				FirstName,
				LastName
			)
			VALUES
			(
				'{$Email}',
				'{$Password}',
				'{$FirstName}',
				'{$LastName}'
			)
		");
	
		$UserID = $DB->getInsertID();
		
		$DB->query("SELECT Email, ID FROM user WHERE ID='{$UserID}' LIMIT 1");
		
		if($DB->num() == 1)
		{
			$fetch = $DB->fetch();
			$return = $fetch;
			$_SESSION['UserID'] = $fetch['ID'];
		}
		else
		{
			$return['Error'] = "Could not register user!";
		}
	}

echo json_encode($return);

require '../$footer.php';
?>